package com.sc.nft.service.impl;

import com.sc.nft.dao.UserEquityPropsDao;
import com.sc.nft.dao.UserEquityPropsLogDao;
import com.sc.nft.entity.UserEquityProps;
import com.sc.nft.entity.UserEquityPropsLog;
import com.sc.nft.service.UserEquityPropsLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 权益道具记录表服务接口实现
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-03-28 10:28:52
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserEquityPropsLogServiceImpl implements UserEquityPropsLogService {
    private final UserEquityPropsLogDao userEquityPropsLogDao;
    private final UserEquityPropsDao userEquityPropsDao;

    private static final int PAGE_SIZE = 1000; // 每页记录数
    private static final int THREAD_COUNT = 5; // 线程数

    @Override
    public void fixData() {
        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);

        Long lastMinId = Long.MAX_VALUE;

        while (true) {
            List<UserEquityPropsLog> records = userEquityPropsLogDao.selectByLastMinId(lastMinId, PAGE_SIZE);
            if (records.isEmpty()) {
                break;
            }

            lastMinId = records.get(records.size() - 1).getId();

            for (UserEquityPropsLog log : records) {
                executorService.submit(() -> fixRecord(log));
            }
        }
        executorService.shutdown();
        try {
            if (!executorService.awaitTermination(1, TimeUnit.HOURS)) {
                executorService.shutdownNow();
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
        }
    }

    private void fixRecord(UserEquityPropsLog log) {
        if (StringUtils.isEmpty(log.getUserEquityPropsId())) {
            return;
        }
        Long firstUserEquityPropsId = Long.valueOf(extractFirstValue(log.getUserEquityPropsId()));
        UserEquityProps userEquityProps = userEquityPropsDao.getById(firstUserEquityPropsId);
        if (userEquityProps != null) {
            UserEquityPropsLog userEquityPropsLog = new UserEquityPropsLog();
            userEquityPropsLog.setId(log.getId());
//            userEquityPropsLog.setPropsId(userEquityProps.getEquityPropsId());
            userEquityPropsLogDao.updateById(userEquityPropsLog);
        }
    }

    /**
     * 从字符串中提取第一个值。
     *
     * @param input 输入字符串
     * @return 提取的第一个值
     */
    public static String extractFirstValue(String input) {
        if (input == null || input.isEmpty()) {
            return null;
        }

        // 处理第三种格式 "1445399->1445399"
        if (input.contains("->")) {
            String[] parts = input.split("->");
            if (parts.length > 0) {
                return parts[0].trim();
            }
        }

        // 处理第一种格式 "1246,1247,1248,1249,1250"
        if (input.contains(",")) {
            String[] parts = input.split(",");
            if (parts.length > 0) {
                return parts[0].trim();
            }
        }

        // 处理第四种格式 "[1437874, 1437875, 1437876, ...]"
        if (input.startsWith("[") && input.endsWith("]")) {
            input = input.substring(1, input.length() - 1); // 去掉开头和结尾的方括号
            String[] parts = input.split(",");
            if (parts.length > 0) {
                return parts[0].trim();
            }
        }

        // 处理第二种格式 "1246"
        return input.trim();
    }


}